package com.toyota.toyotaownerspoc.service;

import java.io.File;
import java.lang.ref.WeakReference;

import utility.FileUtility;

import com.toyota.toyotaownerspoc.R;
import com.toyota.toyotaownerspoc.barcode.ScanVinFromBarcodeActivity;
import com.toyota.toyotaownerspoc.main.ActivitytToyotaOwnersMainListView;
import com.toyota.toyotaownerspoc.options.Settings;
import com.toyota.toyotaownerspoc.takeAPicture.TakeAPictureActivity;
import com.toyota.toyotaownerspoc.util.BitmapWorkerTask;
import com.toyota.toyotaownerspoc.util.ImageUtility;
import com.toyota.toyotaownerspoc.barcode.WhereDoIFindMyVin;
//import com.toyota.toyotaownerspoc.nearby.Nearby;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.os.AsyncTask;
// * HP07 Ability to Navigate to the Service Section: On tap of
// the Service option on the homepage, the user will be redirected
// to the Service section with the Service History page displayed by
// default.
// */
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;

@SuppressLint("NewApi")
public class Service extends Activity {
	// Note: later add items into spinnerYear dynamically , change
	// implementation to add year based on actual model that was selected

	private ImageView HomeHeaderLeftImageView = null;
	private ImageView TakeAPictureImageView = null;
	private ImageView ScanVinActivity = null;
	private ImageView WhereDoIFindMyVin = null;

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		setBackgrounfOfImageView();
		super.onResume();
	}
	/* sets the background of the empty snapshot section on the header of GUI from an image taken by the camera*/
	public void setBackgrounfOfImageView()
	{
	ImageView imageView = null;
	Bitmap bitmapImage  =  null; 
	Drawable drawable  = null; 
		//storage/sdcard0/Pictures
				try {
					File sdDir = Environment
							.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

					String filename = sdDir + File.separator
							+ "MyVehicleBackground.jpg";
					

					Log.d("ClassScanViewBarcodeActivity, onResume() drawable path/filename:",
							filename);
					//get the background regions dimensions
					int requiredWidth = ImageUtility.getImageResourceWidth(this.getResources(),R.drawable.my_toyota_upper);
					int requiredHeight = ImageUtility.getImageResourceHeight(this.getResources(),R.drawable.my_toyota_upper);
					
					Log.d("ClassScanViewBarcodeActivity, setBackgrounfOfImageView() , R.id.imageView3 , requiredWidth: ",String.valueOf(requiredWidth));
					Log.d("ClassScanViewBarcodeActivity, setBackgrounfOfImageView() , R.id.imageView3 , requiredHeight: ",String.valueOf(requiredHeight));
					//
					BitmapWorkerTask newBitmapWorkerTask = new BitmapWorkerTask( filename, requiredWidth, requiredHeight); 
					
					//make the call to decode file synchronous 
					//Bitmap bitmapImage = BitmapFactory.decodeFile(filename);
					
					Thread bitmapWorkerTaskThread = new Thread(newBitmapWorkerTask); 
					bitmapWorkerTaskThread.start();
					bitmapWorkerTaskThread.join();
					
					while (newBitmapWorkerTask.isFlag()!=true)
					{
						Log.d("ClassScanViewBarcodeActivity, setBackgrounfOfImageView() wait(), newBitmapWorkerTask.isFlag():", String.valueOf(newBitmapWorkerTask.isFlag()));
						try
						{
							synchronized (this)
							{
						this.wait();
							}
						}
						catch (InterruptedException e)
						{
							e.printStackTrace();
						}
						
					}
					
					
					bitmapImage = newBitmapWorkerTask.getBitmap();
					
					if (bitmapImage != null) {
						//dirty: double the dimensions in order to makeup for halving for the compression , this makes sure the image is autoresized by the ImageView correctly
						bitmapImage = Bitmap.createScaledBitmap(bitmapImage, bitmapImage.getWidth() *2 , bitmapImage.getHeight() * 2, true);
						imageView = (ImageView) findViewById(R.id.imageView3);
						if (imageView != null) {
							
							drawable = new BitmapDrawable(getResources(),
									bitmapImage);
							//use .setImageDrawable(drawable) to be backward compatible with Android APIs prior API 16 
								imageView.setImageDrawable(drawable);
								imageView.invalidate();
	
							
						} else {
							Log.d("ClassScanViewBarcodeActivity, onResume() imageView:",
									String.valueOf(imageView));
						}
					} else {
						Log.d("ClassScanViewBarcodeActivity, onResume() bitmapImage is :",
								String.valueOf(bitmapImage));
					}

					

					
				} catch (Exception exc) {
					Log.d("ClassScanViewBarcodeActivity, exception:",
							String.valueOf(exc.getMessage()));
					exc.printStackTrace();
				}
			
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_toyota);

		HomeHeaderLeftImageView = (ImageView) findViewById(R.id.imageView1);

		if (HomeHeaderLeftImageView != null) {
			HomeHeaderLeftImageView
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View view) {
							Intent i = new Intent(view.getContext(),
									ActivitytToyotaOwnersMainListView.class);
							startActivity(i);
						}

					});

		}

		TakeAPictureImageView = (ImageView) findViewById(R.id.imageView8);

		if (TakeAPictureImageView != null) {
			TakeAPictureImageView
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View view) {
							Intent i = new Intent(view.getContext(),
									TakeAPictureActivity.class);
							startActivity(i);
						}

					});

		}

		ScanVinActivity = (ImageView) findViewById(R.id.imageView9);

		if (ScanVinActivity != null) {
			ScanVinActivity.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent i = new Intent(view.getContext(),
							ScanVinFromBarcodeActivity.class);
					startActivity(i);
				}

			});

		}

		// WhereDoIFindMyVin

		WhereDoIFindMyVin = (ImageView) findViewById(R.id.imageView10);

		if (WhereDoIFindMyVin != null) {
			WhereDoIFindMyVin.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View view) {
					Intent i = new Intent(
							view.getContext(),
							com.toyota.toyotaownerspoc.barcode.WhereDoIFindMyVin.class);
					startActivity(i);
				}

			});

		}
		
		
		//lunch the activity 
		EditText editText = (EditText)findViewById(R.id.editTextVIN);
		Intent in = getIntent();
		
		if (editText!=null)
		{
			

			if (in!=null)
			{
			String vin=  in.getStringExtra("com.toyota.toyotaownerspoc.globalVIN");
			if (vin!=null && vin.length()>0)
			editText.setText(vin);
			}
		
		}

	}// end onCreate
	
	@Override
	public void onBackPressed(){
		Intent i = new Intent(
				this.getApplicationContext(),
				com.toyota.toyotaownerspoc.main.ActivitytToyotaOwnersMainListView.class);
		startActivity(i);
	}

}
